package cn.edu.scujcc.myprojecttest;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import cn.edu.scujcc.myproject.R;

public class LibraryLoginActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        Window window = getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_SECURE);//清除
//        window.addFlags(WindowManager.LayoutParams.FLAG_SECURE);//开启禁止截屏和录屏
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_library_login);

        //调用windowmanager类隐藏状态栏

        //android4.1以下使用WindowManager
        //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        //4.1以上
        //Google文档
//        View decorView = getWindow().getDecorView();
//        // Hide the status bar.
//        int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
//        decorView.setSystemUiVisibility(uiOptions);
        //隐藏状态栏
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);

        //隐藏action bar
        ActionBar acbar = getSupportActionBar();
        acbar.hide();

        //设置透明状态栏
//        if(Build.VERSION.SDK_INT >= 21) {
//            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
//                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
//            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
//            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//            window.setStatusBarColor(Color.TRANSPARENT);
//            window.setNavigationBarColor(Color.TRANSPARENT);
//        }
        //当textview获取焦点时，隐藏hint当内容，具体是重写OnFocusChange方法
        //username输入框

        //根据password textview的输入状态，调整是否显示 有颜色的登录button>>>    具体是继承TextWatcher类，重写 onTextChanged方法
        EditText passInput = findViewById(R.id.YktPassword);
        Button loginColor = findViewById(R.id.yktlogin_color);
        passInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String pass = passInput.getText().toString();
                if (TextUtils.isEmpty(pass)){
                    loginColor.setVisibility(View.INVISIBLE);
                }else {
                    loginColor.setVisibility(View.VISIBLE);
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        //获取textview焦点状态，当获取到输入焦点时隐藏 hint 文本内容
        EditText username = findViewById(R.id.YktUsername);
        username.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                EditText textView = (EditText) v;
                String hint;
                if (hasFocus){
                    hint = textView.getHint().toString();
                    textView.setTag(hint);//在获取到焦点时先把原始hint文本保存到Tag，再将文本设置为空 ""  以Tag的形式捆绑在View（即获得焦点的EditText）上
                    textView.setHint("");
                }else {
                    hint = textView.getTag().toString();
                    textView.setHint(hint);
                }

            }
        });
        //password输入框
        EditText password = findViewById(R.id.YktPassword);
        password.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                EditText textView = (EditText) v;
                String hint;
                if (hasFocus){
                    hint = textView.getHint().toString();
                    textView.setTag(hint);
                    textView.setHint("");
                }else {
                    hint = textView.getTag().toString();
                    textView.setHint(hint);
                }

            }
        });

        //点击登录按钮  >>>   跳转事件
        Button loginButton = findViewById(R.id.yktlogin_color);
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //实例化EditText组件对象，将text转化为string
                EditText textView = findViewById(R.id.YktUsername);
                String username = textView.getText().toString();
                EditText textView1 = findViewById(R.id.YktPassword);
                String password = textView1.getText().toString();

//              Intent intent = new Intent(LibraryLogin.this,MainActivity.class);
                //利用intent意图跳转到第二个页面
                Intent intent = new Intent();
                intent.setClass(LibraryLoginActivity.this, LibraryUserInfoActivity.class);
                //利用intent传参
                intent.putExtra("username",username);
                intent.putExtra("password",password);
                startActivity(intent);
                //提示框
//                new AlertDialog.Builder(LibraryLogin.this).setTitle("登录").setMessage("账号："+username+"\r\n密码："+password).setNegativeButton("确定",null).show();



            }
        });


        //底部button跳转到登录一卡通activity
//        Button toYkt = findViewById(R.id.button_to_ykt);
//        toYkt.setOnClickListener(new View.OnClickListener() {
//
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent();
//                intent.setClass(LibraryLoginActivity.this, YktLoginActivity.class);
//                startActivity(intent);
//            }
//        });
    }
}
